<template>
  <el-menu
    :default-active="activeIndex"
    class="navbar"
    mode="horizontal"
    @select="handleSelect"
    router
  >
    <el-menu-item index="/dashboard" class="brand">
      <el-icon><Setting /></el-icon>
      <span>工单管理系统</span>
    </el-menu-item>
    
    <div class="nav-links">
      <el-menu-item 
        v-if="authStore.isAuthenticated && ['admin','agent','manager'].includes(authStore.role)"
        index="/statistics-dashboard">
        <el-icon><DataAnalysis /></el-icon>
        <span>统计报表</span>
      </el-menu-item>
    </div>

    <div class="nav-right">
      <!-- 通知图标 -->
      <el-badge :value="notificationsStore.unreadCount" :hidden="notificationsStore.unreadCount === 0" class="notification-badge">
        <el-button 
          type="text" 
          @click="goToNotifications"
          class="notification-btn"
          title="通知中心"
        >
          <el-icon><Bell /></el-icon>
        </el-button>
      </el-badge>
      
      <el-dropdown @command="handleUserCommand">
        <span class="user-info">
          <el-icon><User /></el-icon>
          <span>{{ authStore.userDisplayName }}</span>
          <el-icon><ArrowDown /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="profile">
              <el-icon><User /></el-icon>
              个人中心
            </el-dropdown-item>
            <el-dropdown-item command="settings">
              <el-icon><Setting /></el-icon>
              系统设置
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      
      <el-button 
        type="danger" 
        plain 
        @click="handleLogout"
        class="logout-btn"
      >
        <el-icon><SwitchButton /></el-icon>
        退出登录
      </el-button>
      
      <ThemeToggle style="margin-left: 12px;" />
    </div>
  </el-menu>
</template>

<script>
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore, PERMISSIONS } from '../store/auth'
import { useNotificationsStore } from '../store/notifications'
import ThemeToggle from './ThemeToggle.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import ChartCleanup from '../utils/chartCleanup'
import { DataAnalysis, Setting, Bell, User, ArrowDown, SwitchButton } from '@element-plus/icons-vue'

export default {
  name: 'AppNavbar',
  components: {
    ThemeToggle
  },
  setup() {
    const route = useRoute()
    const router = useRouter()
    const authStore = useAuthStore()
    const notificationsStore = useNotificationsStore()
    
    const activeIndex = computed(() => {
      const path = route.path
      if (path.startsWith('/dashboard')) return '/dashboard'
      // 其他功能模块已移除，只保留登录相关功能
      return '/dashboard'
    })

    const handleSelect = () => {
      // 路由跳转由 router 属性自动处理
    }

    const handleUserCommand = async (command) => {
      switch (command) {
        case 'profile':
          // 跳转到个人中心页面
          router.push('/personal-center')
          break
        case 'settings':
          ElMessage.info('系统设置功能开发中...')
          break
      }
    }

    const goToNotifications = () => {
      router.push('/notifications')
    }

    const handleLogout = async () => {
      try {
        await ElMessageBox.confirm(
          '确定要退出登录吗？',
          '确认退出',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
        
        // 清理所有图表实例
        await ChartCleanup.cleanupAllCharts()
        
        authStore.logout()
        ElMessage.success('已退出登录')
        
        // 延迟跳转，确保清理完成
        setTimeout(() => {
          router.push('/login')
        }, 100)
      } catch (error) {
        // 用户取消或其他错误
        if (error !== 'cancel') {
          console.error('Logout error:', error)
        }
      }
    }

    return { 
      activeIndex, 
      handleSelect, 
      handleUserCommand,
      goToNotifications,
      handleLogout,
      authStore,
      notificationsStore,
      PERMISSIONS
    }
  }
}
</script>

<style scoped>
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 40px;
  height: 80px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  position: sticky;
  top: 0;
  z-index: 1000;
}

.brand {
  font-size: 24px;
  font-weight: 800;
  color: #1a1a1a;
  border-bottom: none !important;
  background: transparent !important;
  display: flex;
  align-items: center;
  gap: 12px;
  letter-spacing: -0.01em;
}

.brand:hover {
  background: transparent !important;
  color: #1a1a1a;
  transform: scale(1.02);
}

.nav-links {
  display: flex;
  flex: 1;
  justify-content: center;
  /* 功能模块已移除，保持布局平衡 */
}

.nav-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 20px;
}

.notification-badge {
  margin-right: 8px;
}

.notification-btn {
  font-size: 18px;
  color: #666;
  transition: color 0.2s;
}

.notification-btn:hover {
  color: #007bff;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 12px;
  cursor: pointer;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: var(--bg-tertiary);
}

.logout-btn {
  margin-left: 12px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.logout-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(245, 108, 108, 0.3);
}

:deep(.el-menu--horizontal .el-menu-item) {
  border-bottom: none;
  background: transparent !important;
  color: var(--text-primary);
}

:deep(.el-menu--horizontal .el-menu-item:hover) {
  background: var(--bg-tertiary) !important;
  color: var(--text-primary);
}

:deep(.el-menu--horizontal .el-menu-item.is-active) {
  border-bottom: 2px solid #409eff;
  color: #409eff;
  background: transparent !important;
}
</style>